/*
 * @module: 
 * @Descripttion: 
 * @Author: guoyu
 * @Date: 2022-06-30 16:05:32
 * @LastEditors: wangshengjin
 * @LastEditTime: 2022-07-05 11:14:53
 */
'use strict';
const Controller = require('egg').Controller;
const fs = require('fs');
const path = require('path');

class CreateDbsController extends Controller {
  async index() {
    const { ctx } = this;
    const { query } = ctx;
    let { subjectId } = query;
    const trueSubjectId = subjectId; // 真实的id
    const subMap = {
      2: 1,
      3: 2,
      10: 3,
      11: 3
    }
    subjectId = subMap[subjectId];
    const setDb = async (ctx, index, type) => {
      let response = await ctx.service.syncServer.index({ type: type, page: index, per: 1000 });
      if (response.code == 200) {
        if (index <= response._meta.pageCount) {
          index = index + 1;
          await Promise.all(response.data.map(async (item) => {
            const res = await ctx.service.syncServer.create(item, type, trueSubjectId);
            return item;
          }))
          if (index <= response._meta.pageCount) {
            const result = await setDb(ctx, index, type);
            if (result) {
              return true;
            }
          } else {
            return true;
          }
        }
      }
    }

    const treeFilter = (json) => {
      json.data.map((item) => { // 遍历初中 高中
        item.children.map((grade) => { // 遍历教材
          grade.children.map((book) => {// 遍历年级
            if (book.icon) {
              book.icon = `/icon/${path.basename(book.icon)}`;
            }
            return book;
          })
          return grade;
        })
        return item;
      })
      return json;
    }
    if (fs.existsSync('./dbs/model/nobook.db')) {
      await this.app.model.drop();
    }
    await this.app.model.sync({ alter: true }); //force false 为不覆盖 true会删除再创建 alter true可以 添加或删除字段;
    const categoryIndex = 1;
    const experimentIndex = 1;
    const kpIndex = 1;
    const kmIndex = 1;
    const res1 = await setDb(ctx, categoryIndex, 'experiment_category');
    const res2 = await setDb(ctx, experimentIndex, 'experiment');
    const res3 = await setDb(ctx, kpIndex, 'knowledge_points');
    const res4 = await setDb(ctx, kmIndex, 'knowledge_emperiment_map');
    const [tree, kpTree1, kpTree2, kpTree3] = await Promise.all([
      ctx.service.treeServer.index(subjectId),
      ctx.service.treeServer.knowledgeTree(subjectId, 1),
      ctx.service.treeServer.knowledgeTree(subjectId, 2),
      ctx.service.treeServer.knowledgeTree(subjectId, 3)
    ])
    let treeStr = '';
    let treeStr1 = '';
    let treeStr2 = '';
    let treeStr3 = '';
    if (tree.code == 200) {
      const resTreeStr = treeFilter(tree);
      treeStr = JSON.stringify(resTreeStr);
    }
    if (kpTree1.code) {
      const resTreeStr1 = treeFilter(kpTree1);
      treeStr1 = JSON.stringify(resTreeStr1);
    }
    if (kpTree2.code) {
      const resTreeStr2 = treeFilter(kpTree2);
      treeStr2 = JSON.stringify(resTreeStr2);
    }
    if (kpTree3.code) {
      const resTreeStr3 = treeFilter(kpTree3);
      treeStr3 = JSON.stringify(resTreeStr3);
    }
    await Promise.all([
      tree.code == 200 && fs.writeFileSync('./dbs/model/tree.json', treeStr),
      kpTree1.code == 200 && fs.writeFileSync('./dbs/model/kpTree1.json', treeStr1),
      kpTree2.code == 200 && fs.writeFileSync('./dbs/model/kpTree2.json', treeStr2),
      kpTree3.code == 200 && fs.writeFileSync('./dbs/model/kpTree3.json', treeStr3)
    ])
    ctx.body = {
      code: 200,
      msg: 'ok'
    };
  }
}

module.exports = CreateDbsController;
